package org.moonlight.jvm.classpath.entry;

import lombok.EqualsAndHashCode;
import org.moonlight.jvm.classpath.Entry;

import java.io.IOException;
import java.nio.file.*;

/**
 * ZIP或JAR文件形式的类路径
 *
 * @author Moonlight
 * @createTime 2023/8/14 15:12
 **/
@EqualsAndHashCode
public class ZipEntry implements Entry {

    /** ZIP或JAR文件的绝对路径 **/
    private final Path absPath;

    public ZipEntry(String path) {
        this.absPath = Paths.get(path).toAbsolutePath();
    }

    @Override
    public byte[] readClass(String className) throws IOException {
        try (FileSystem fileSystem = FileSystems.newFileSystem(this.absPath, null)) {
            return Files.readAllBytes(fileSystem.getPath(className));
        }
    }

    @Override
    public String toString() {
        return this.absPath.toString();
    }
}
